<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="deptRules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width: 80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width: 80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
          <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width: 80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartment, getDepartDetail, updateDepartment } from '@/api/departments'
export default {
  props: {
    pid: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    // eslint-disable-next-line vue/require-default-prop
    originList: {
      rewquired: true,
      type: Array
    }
  },
  data() {
    // 编号校验
    const validCode = (ruls, value, callback) => {
      // 拿到所有部门的编号
      let existCode = this.originList.map(item => item.code)
      console.log(existCode)
      // 编辑状态
      if (this.isEdit) {
        // 获取到当前正在编辑的部门编号 排除在外
        const curCode = this.originList.find(item => item.id === this.pid).code
        existCode = existCode.filter(item => item !== curCode)
      }
      // 判断输入的编号是否存在
      existCode.includes(value) ? callback(new Error('此编码已存在，请更换')) : callback()
    }
    // 部门校验
    const validName = (ruls, value, callback) => {
      // 筛选出当前选中的子部门
      let existName = this.originList.filter(item => item.pid === this.pid).map(item => item.name)
      console.log(existName)
      // 编辑状态
      if (this.isEdit) {
        // 1. 找到当前被编辑的元素
        const dept = this.originList.find(item => item.id === this.pid)
        // 2. 取出pid(它的父级是谁)
        const pid = dept.pid
        // 3. 过滤： 同一个父级，不包括自己； map： 只要需要名字
        existName = this.originList.filter(item => item.pid === pid && item.id !== this.pid).map(item => item.name)
        console.log(existName)
      }
      // 判断输入的编号是否存在
      existName.includes(value) ? callback(new Error('此部门名称已存在，请更换')) : callback()
    }
    return {
      list: [], // 所欲员工信息
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: this.pid
      },
      deptRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }

        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.loadEmployees()
    // 如果当前时编辑，发请求获取部门详情
    if (this.isEdit) {
      this.loadDetail()
    }
  },
  methods: {
    // 获取部门详情
    async loadDetail() {
      const { data: res } = await getDepartDetail(this.pid)
      // console.log(`res,${res}`)
      // 数据回填
      this.form = res
    },
    // 加载下拉菜单的员工
    async loadEmployees() {
      const { data: res } = await getEmployeeSimple()
      // console.log('部门负责人', res)
      this.list = res
    },
    // 添加部门
    async doAdd() {
      await addDepartment(this.form)
      // 自定义的事件，通知父组件关闭弹窗
      this.$emit('success')
      // 清空表单
      this.$refs.deptForm.resetFields()
      this.$message.success('添加部门成功')
    },
    // 修改部门
    async doEdit() {
      await updateDepartment(this.form)
      // 自定义的事件，通知父组件关闭弹窗
      this.$emit('success')
      this.$message.success('修改部门成功')
    },
    hSubmit() {
      // 表单校验
      this.$refs.deptForm.validate(valid => {
        if (!valid) return
        // 根据 isEdit的值, 判断 做添加还是修改
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    }
  }
}
</script>

<style></style>
